package com.travel.server.manager.impl.command.data;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.data.QueryTradeListRequest;
import com.travel.server.manager.dto.response.data.QueryTradeListResponse;
import com.travel.server.manager.dto.response.data.QueryTradeListResponse.TradeItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.OrderDal;
import com.travel.server.manager.model.dto.data.TradeDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/14 19:14
 * @Desc:
 */
@Component
@Slf4j
public class QueryTradeListCommand extends AbstractCommand<QueryTradeListRequest, QueryTradeListResponse> {

    @Autowired
    private OrderDal orderDal;

    @Override
    protected void checkInput(QueryTradeListRequest request) {
        checkTimeInput(request);
        checkPageInput(request);
    }

    @Override
    public QueryTradeListResponse handle(QueryTradeListRequest request) {
        checkInput(request);
        Date startTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());
        int total = orderDal.selectTradeCount(startTime, endTime);

        QueryTradeListResponse response = new QueryTradeListResponse();
        response.setTotalNum(total);
        response.setTradeList(makeList(startTime, endTime, request.getOffset(), request.getLimit()));
        return response;
    }

    public List<TradeItem> makeList(Date startTime, Date endTime, Integer offset, Integer limit){
        List<TradeDto> list = orderDal.selectTradeList(startTime, endTime, offset, limit);
        List<TradeItem> tradeList = new ArrayList<>();
        if(!CollectionUtil.isEmpty(list)){
            for(TradeDto dto : list){
                TradeItem item = new TradeItem();
                item.setId(dto.getId());
                item.setCustomerName(dto.getCustomerName());
                item.setOrderAmount(CommonUtil.fenToYuan(dto.getOrderAmount()).toString());
                item.setOrderDesc(dto.getOrderName());
                item.setOrderId(dto.getOrderId());
                item.setRemark(dto.getNote());
                item.setTradeDate(dto.getOrderTime());
                item.setPayStatus("");
                tradeList.add(item);
            }
        }
        return tradeList;
    }
}
